﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InfoPick.Lib.Tools;

namespace InfoPick.Lib.StringResolve
{
    public class BRDResolver
    {
        public static List<GMDDF> ResolveGMDDF(string str)
        {
            List<GMDDF> list = new List<GMDDF>();
            if (!string.IsNullOrEmpty(str))
            {
                const string startIdentifier = "槽位记录表";
                const string endIdentifier = "(结果个数";
                const string split = "\r\n\r\n";
                const string startWith = "机框号 ";

                var formList = StringTools.StrFilter(str, startIdentifier, endIdentifier, 0, new string[] { split }, startWith);

                foreach (var item in formList)
                {
                    try
                    {
                        var table = StringTools.StrFilter(item, "", "", 0, new string[] { "\r\n" });

                        var dict = StringTools.StrTable2Dict(table, new string[] { "=" });

                        GMDDF obj = new GMDDF();
                        obj.Frame = int.Parse(dict["机框号"]);
                        obj.SLotNum = int.Parse(dict["槽位号"]);
                        obj.BoardNum = int.Parse(dict["板组号"]);
                        list.Add(obj);
                    }
                    catch (Exception ex)
                    {
                        string message = string.Format("{0} : message:{1}\r\n{2}", DateTime.Now, ex.Message, ex.StackTrace);
                        Log.Log.WriteLog(message);
                    }
                }

            }
            return list;
        }

        public static List<GMODF> ResolveGMODF(string str)
        {
            List<GMODF> list = new List<GMODF>();
            if (!string.IsNullOrEmpty(str))
            {
                const string startIdentifier = "槽位记录表";
                const string endIdentifier = "(结果个数";
                const string split = "\r\n\r\n";
                const string startWith = "机框号 ";

                var formList = StringTools.StrFilter(str, startIdentifier, endIdentifier, 0, new string[] { split }, startWith);

                foreach (var item in formList)
                {
                    try
                    {
                        var table = StringTools.StrFilter(item, "", "", 0, new string[] { "\r\n" });

                        var dict = StringTools.StrTable2Dict(table, new string[] { "=" });

                        GMODF obj = new GMODF();
                        obj.Frame = int.Parse(dict["机框号"]);
                        obj.SLotNum = int.Parse(dict["槽位号"]);
                        obj.BoardNum = int.Parse(dict["板组号"]);

                        //筛选出 ("1+1备份"&&"主备用") 和 ("负荷分担")
                        if (dict["备份状态"] == "1+1 备份")
                        {
                            if (dict["主备状态"] == "主用")
                                obj.IsPrimary = true;
                            if (dict["主备状态"] == "备用")
                                obj.IsPrimary = false;
                        }
                        list.Add(obj);
                    }
                    catch (Exception ex)
                    {
                        string message = string.Format("{0} : message:{1}\r\n{2}", DateTime.Now, ex.Message, ex.StackTrace);
                        Log.Log.WriteLog(message);
                    }
                }
            }

            return list;
        }

        public static List<GSDDF> ResolveWEPIDDF(string str)
        {
            List<GSDDF> list = new List<GSDDF>();
            if (!string.IsNullOrEmpty(str))
            {
                var table = StringTools.GetTable(str, "单板信息", "(结果个数");
                foreach (var item in table)
                {
                    GSDDF obj = new GSDDF();
                    obj.Frame = int.Parse(item[0]);
                    obj.SLotNum = int.Parse(item[1]);
                    list.Add(obj);
                }
            }

            return list;
        }

        public static List<GSDDF> ResolveWCSUDDF(string str)
        {
            List<GSDDF> list = new List<GSDDF>();
            if (!string.IsNullOrEmpty(str))
            {
                var table = StringTools.GetTable(str, "单板信息", "(结果个数");
                foreach (var item in table)
                {
                    GSDDF obj = new GSDDF();
                    obj.Frame = int.Parse(item[0]);
                    obj.SLotNum = int.Parse(item[1]);
                    obj.CSUNum = int.Parse(item[4]);
                    list.Add(obj);
                }
            }


            return list;
        }
    }

}
